package com.itheima.demo;

import com.itheima.domain.Course;
import com.itheima.domain.Student;
import com.itheima.mapper.Many2ManyMapper;
import com.itheima.utils.MapperUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class Many2ManyDemo {

    //创建工具类对象
    public static MapperUtils mapperUtils = new MapperUtils();

    public static void main(String[] args) throws IOException {
        //通过工具类获取SqlSession对象
        SqlSession sqlSession = mapperUtils.getSqlSession("MyBatisConfig.xml");
        //获取映射对象
        Many2ManyMapper mapper = sqlSession.getMapper(Many2ManyMapper.class);
        //调用方法
        List<Student> students = mapper.findStudentAndCourse();
        System.out.println("======================");
        for (Student student : students) {
            System.out.println(student.getName() + ", " + student.getAge());
            List<Course> courses = student.getCourses();
            for (Course c : courses) {
                System.out.println("\t" + c.getName());
            }
        }
        System.out.println("======================");
        //释放资源
        mapperUtils.closeResource();
    }
}
